/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.app.content;

import han.bifang.gwt.client.app.AppContentConstants;
import han.bifang.gwt.client.app.AppMessages;
import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.PrivilegChecker;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.Application;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;

import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;

/**
 * 
 * @Author Kent
 */
public class AppDetailContent extends Content {
	private AppMessages cons;
	private BifangGWTServiceAsync service;
	private String appId = null;
	
	private Application dd = null;
	private GwtContext context;

	private VerticalPanel panel = null;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			dd = (Application) res.getResponse();
			//Window.alert("" + dd.getBuses().size() + " " + dd.getCatalog());
			
			
			HTML h = new HTML(cons.appBasic());
			panel.add(h);
			// show domain attributes
			Grid table = new Grid(2, 3);
			table.setCellPadding(1);
			table.setCellSpacing(1);
			table.setBorderWidth(1);
			
			ColumnFormatter cf = table.getColumnFormatter();
			cf.setWidth(0, "15%");
			cf.setWidth(0, "15%");
			cf.setWidth(1, "45%");

			table.setHTML(0, 0, cons.name());
			table.setHTML(0, 1, cons.code());
			table.setHTML(0, 2, cons.desc());

			RowFormatter rf = table.getRowFormatter();
			rf.setStyleName(0, "tableheader");

			table.setHTML(1, 0, dd.getName());
			table.setHTML(1, 1, dd.getCode());
			table.setHTML(1, 2, dd.getDescription());
			
			panel.add(new HTML("&nbsp;"));
			panel.add(table);
			HorizontalPanel h1 = new HorizontalPanel();
			//h1.setSpacing(15);
			Button b1 = new Button(cons.modify()), b2 = new Button(cons
					.remove());
			h1.add(b1);
			h1.add(b2);
			b1.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					HashMap props = new HashMap();
					props.put("app", dd);
					listener.onContentChanged(
							AppContentConstants.APP_DETAIL,
							AppContentConstants.MODIFY_APP, props);

				}

			});

			b2.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					HashMap props = new HashMap();
					props.put("app", dd);
					listener.onContentChanged(
							AppContentConstants.APP_DETAIL,
							AppContentConstants.REMOVE_APP, props);

				}

			});
			panel.add(new HTML("&nbsp;"));
			if(PrivilegChecker.hasPrivilege(context,null))
				panel.add(h1);

			
		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}

	};

	

	public AppDetailContent(AppMessages cons,
			BifangGWTServiceAsync service, String appId,GwtContext context) {
		this.cons = cons;
		this.service = service;
		this.appId = appId;
		this.context = context;
		
		
		panel = new VerticalPanel();
		panel.setSpacing(5);
		panel.setWidth("100%");

		initWidget(panel);

	}

	public void onLoad() {

		RequestObject req = new RequestObject();
		req.setAction("han.bifang.gwt.server.action.app.GetAppDetailAction");
		req.setRequest(appId);
		service.call(req, cb);
		
	}

}
